package com.stupidzhe.vbreview.controller.adm;

import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.cache.CaptchaCache;
import com.stupidzhe.vbreview.cache.TeacherCache;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.TeacherBaseService;
import com.stupidzhe.vbreview.util.CookieUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Controller
@RequestMapping("/adm")
public class AdmBaseController  {

    private final
    TeacherBaseService teacherBaseService;

    private final
    CaptchaCache captchaCache;

    private final
    TeacherCache teacherCache;

    public AdmBaseController(TeacherBaseService teacherBaseService, CaptchaCache captchaCache, TeacherCache teacherCache) {
        this.teacherBaseService = teacherBaseService;
        this.captchaCache = captchaCache;
        this.teacherCache = teacherCache;
    }

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String mainView() {
        return "adm/adm.index";
    }

    @RequestMapping(value = "/home", method = RequestMethod.GET)
    public String homeView() {
        return "adm/adm.home";
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public String loginView(HttpServletRequest request, HttpServletResponse response) {
        return "adm/adm.login";
    }


    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultBean login(@RequestParam(value = "account", required = false)String account,
                            @RequestParam(value = "password", required = false)String password,
                            @RequestParam(value = "captcha", required = false)String captcha,
                            HttpServletRequest request, HttpServletResponse response) {
        String realCaptcha = captchaCache.getCaptcha(request.getSession().getId());
        captchaCache.removeCaptcha(request.getSession().getId());
        if (realCaptcha == null) {
            return new ResultBean(false, "验证码过期，请刷新验证码");
        }
        if (!realCaptcha.equalsIgnoreCase(captcha)) {
            System.out.println(captcha + ":" + realCaptcha);
            return new ResultBean(false, "验证码错误");
        }
        if (null == account || 5 > account.length()) {
            return new ResultBean(false, "用户或密码错误");
        }
        if (null == password || 6 > password.length()) {
            return new ResultBean(false, "用户或密码错误");
        }
        String res = teacherBaseService.login(account, password, request);
        if (!"".equals(res)) {
            // cookie格式: account|md5(account)
            Cookie cookie = new Cookie("t_cookie", account + "|" + res);
            cookie.setMaxAge(86400);
            cookie.setPath("/");
            cookie.setDomain("");
            cookie.setHttpOnly(true);
            response.addCookie(cookie);
            return new ResultBean(true, "登录成功");
        }
        return new ResultBean(false, "账号或密码错误");

    }

    @ResponseBody
    @RequestMapping(value = "/logout", method = RequestMethod.DELETE)
    public ResultBean logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Cookie cookie = CookieUtil.getCookieByName(request, "t_cookie");
        if (cookie == null) {
            return new ResultBean(true);
        }
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return new ResultBean(true);
    }

    @RequestMapping(value = "/status/check")
    public String checkView() {
        return "adm/adm.check";
    }

}
